Code Snippet C - Freeing the linked list

Malloc in C Explained

Created: 2022-06-25
Tags: #permanent


TIP: You NEED to free all memory first before reusing a linked list.

while (list != NULL)
{
    node *tmp = list->next;
    free(list);
    list = tmp;
}

Why it's important to free memory?

Memory leaks don't automatically cause problems
-> until you've run out of memory and your call to malloc suddenly fails

References